package com.test.jbpm;

import java.util.Set;

import com.test.jbpm.domain.Dept;

public class CheckboxTreeMaker extends AbstractPermissionTreeMaker {

	public CheckboxTreeMaker(Dept firstLevelNode,
			Set<Dept> permissionIdSet) {
		super(firstLevelNode, permissionIdSet);
	}

	private StringBuilder sbXml = new StringBuilder();
	
	@Override
	protected String doMake(Dept firstLevelNode) {
		make(firstLevelNode);
		return sbXml.toString();
	}	
	
	//传一个对象过来
	private void make(Dept permission) {
		sbXml.append("<item text=\"");
		sbXml.append(permission.getName());
		sbXml.append("\" id=\"");
		sbXml.append(permission.getId());
		sbXml.append("\"");
		if (permission.getChildren().size() == 0) { //如果是叶子
			if (permissionIdSet.contains(permission.getId())) {
				sbXml.append(" checked=\"1\"");
			}
		} else {
			sbXml.append(" open=\"1\"");	
		}		
		sbXml.append(">");
		Set<Dept> childNodes = permission.getChildren();
		//遍历子节点
		for (Dept child : childNodes) {
			make(child);
		}
		sbXml.append("</item>");
	}

}
